﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using application1.User;
using System.Threading;
using application1.Tools;

namespace application1.ThreadManage
{
    class ListenerThread
    {
        public static void ListenClientConnect()
        {
            TcpClient NewClient = null;
            while (true)
            {
                try
                {
                    NewClient = Program.MyListener.AcceptTcpClient();
                }
                catch (Exception)
                {
                    //当MyListener.Stop()的时候，引发此
                    break;
                }

                //if (Program.UserList.Count > 0)         //如果客户端链接里面有东西
                //{
                //    for (int i = 0; i < Program.UserList.Count; i++)
                //    {
                //        Program.UserList[i].Close();        //关闭之前的链接
                //    }
                //}
                UserClass UserClient = new UserClass(NewClient);
                
                Thread ClientReceiveThread = new Thread(new ThreadStart(UserClient.ReciveData));
                UserClient.UserThread = ClientReceiveThread;
                UserClient.UserThread.IsBackground = true;
                UserClient.UserThread.Start();
                Program.UserList.Add(UserClient);
                UserClient.ListIndexID = Program.UserList.IndexOf(UserClient);
                UserClient.SendData("欢迎来到我的服务器\r\n");




            }
        }


        public static void CheckConnctTimeOut()
        {
            while (true)
            {
                //先睡眠10分钟
                Thread.Sleep(1000 * 5);
                Console.WriteLine("进入检查线程");
                for (int i = 0; i < Program.UserList.Count; i++)
                {
                    
                    //对于单片机连接，如果大于10分钟没有有效字符的通信了，就做停止线程工作
                    if ((DateTime.Now - Program.UserList[i].LastConnTime).TotalSeconds > 5)
                    {
                        Console.WriteLine("终止超时设备长链接，具体查LOG文件日志" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                        if (Program.UserList[i].TempData==null || Program.UserList[i].TempData.Length < 1 )    //表明该线程没有发东西，直接关闭掉
                        {
                            Log.Info("ListenerThread", "线程ID=" + Program.UserList[i].UserThread.ManagedThreadId.ToString() + ";终止超时设备长连接，终止的对应设备MeterIP=0");
                            Program.UserList[i].Close();
                        }
                        else    //表明需要重发
                        {
                            
                            if (Program.UserList[i].ReSentTimes >= 6)
                            {
                                //表明重发了3次了，已经出错，直接关闭掉线程
                                Log.Info("重发", "线程ID=" + Program.UserList[i].UserThread.ManagedThreadId.ToString() + ";重发超过3次而关闭,DeviceId="+ Program.UserList[i].DeviceId);
                                Console.WriteLine("重发超3次关闭");
                                Program.UserList[i].Close();
                            }
                            else
                            {
                                Console.WriteLine("线程ID=" + Program.UserList[i].UserThread.ManagedThreadId.ToString()+",检测到需要重发,DeviceId="+ Program.UserList[i].DeviceId);
                                Program.UserList[i].ReSentTimes++;
                                Program.UserList[i].SendDataByte(Program.UserList[i].TempData);
                                Log.Info("重发", "线程ID=" + Program.UserList[i].UserThread.ManagedThreadId.ToString() + ",检测到需要重发,发送数据为:"+CRC.ToHexString(Program.UserList[i].TempData)+",DeviceId="+ Program.UserList[i].DeviceId);
                            }
                            
                        }

                    }
                }
                Console.WriteLine("退出检查线程");

            }
        }


    }
}